package operations;

import book.BookList;

import java.util.Scanner;

/*
   思路：1.判空，如果书架为空的话，给出提示信息，并退出
        2.让用户输入要删除的书本的名字，在书架中查找这本书，如果查找不到，则给出提示信息
        3.删除对应的图书信息
        4.让书架对应的存储数目 -1
 */
public class DeleteOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        //首先判空
        if(bookList.getUsedSize() == 0) {
            System.out.println("书架为空，无法删除图书信息");
            return;
        }

        //遍历查找
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的图书信息");
        String inputBookName = sc.nextLine();
        int i = 0;
        for (; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].getName().equals(inputBookName)) {
                //执行删除操作
                for (int j = i; j < bookList.getUsedSize(); j++) {
                    bookList.getBooks()[j] = bookList.getBooks()[j+1];
                }
                break;
            }

        }

        if(i == bookList.getUsedSize()) {
            System.out.println("未查找到您要删除的图书信息");
            return;
        }

        int currentSize = bookList.getUsedSize();
        bookList.getBooks()[currentSize] = null;
        bookList.setUsedSize(currentSize - 1);
        System.out.println("删除成功");

    }
}
